﻿$(document).ready(function () {
    $('#addcomments').bind('blur', function () {
        var line = "-------------------------------------------------------";
        var myDate = new Date();
        var myFormattedDate2 = myDate.f("dd/MM/yyyy HH:mm:ss");
        $("#DetailsView1_Comments").prepend($("#Header_lblUser").text() + ' -  ' + myFormattedDate2 + ' - ' + $('#addcomments').val() + '\n' + line + '\n');
    });




    
    if ($('#DetailsView3_SelGAS_CUSTOMER_AUTH').val() == '') {
        $('#DetailsView3_btnRPF').hide();
        $('#DetailsView3_btnAddRPF').hide();
        $('#DetailsView3_GAS_CUSTOMER_AUTH_DATE').hide();
        $('#DetailsView3_lblGAS_CUSTOMER_AUTH_DATE').hide();
    }
    else if ($('#DetailsView3_SelGAS_CUSTOMER_AUTH').val() == 'Yes') {
        $('#DetailsView3_btnRPF').show();
        $('#DetailsView3_btnAddRPF').show();
        $('#DetailsView3_GAS_CUSTOMER_AUTH_DATE').show();
        $('#DetailsView3_lblGAS_CUSTOMER_AUTH_DATE').show();
    }

    if ($('#DetailsView2_SelELEC_CUSTOMER_AUTH').val() == '') {
        $('#DetailsView2_btnRPF').hide();
        $('#DetailsView2_btnAddRPF').hide();
        $('#DetailsView2_ELEC_CUSTOMER_AUTH_DATE').hide();
        $('#DetailsView2_lblELEC_CUSTOMER_AUTH_DATE').hide();
    }
    else if ($('#DetailsView2_SelELEC_CUSTOMER_AUTH').val() == 'Yes') {
        $('#DetailsView2_btnRPF').show();
        $('#DetailsView2_btnAddRPF').show();
        $('#DetailsView2_ELEC_CUSTOMER_AUTH_DATE').show();
        $('#DetailsView2_lblELEC_CUSTOMER_AUTH_DATE').show();
    }

    if ($('#DetailsView2_SelELEC_GREEN_ENERGY_REQUIREMENT').val() == '') {
        $('#DetailsView2_ELEC_GREEN_ENERGY_PERCENT').hide();
        $('#DetailsView2_lblELEC_GREEN_ENERGY_PERCENT').hide();
    }
    else if ($('#DetailsView2_SelELEC_GREEN_ENERGY_REQUIREMENT').val() == 'Yes') {
        $('#DetailsView2_ELEC_GREEN_ENERGY_PERCENT').show();
        $('#DetailsView2_lblELEC_GREEN_ENERGY_PERCENT').show();
    }

    //both
    if ($('#DetailsView1_selELEC_STAT').val() == 'Customer lost  have accepted offer with another retailer' || $('#DetailsView1_selELEC_STAT').val() == 'Customer converted / accepted offer' || $('#DetailsView1_selELEC_STAT').val() == 'Customer currently with another retailer') {
        document.getElementById('div_STTS_ELEC').style.display = 'inline';
    }
    else {
        document.getElementById('div_STTS_ELEC').style.display = 'none';
    }

    if ($('#DetailsView1_selGAS_STAT').val() == 'Customer lost  have accepted offer with another retailer' || $('#DetailsView1_selGAS_STAT').val() == 'Customer converted / accepted offer' || $('#DetailsView1_selELEC_STAT').val() == 'Customer currently with another retailer') {
        document.getElementById('div_STTS_GAS').style.display = 'inline';
    }
    else {
        document.getElementById('div_STTS_GAS').style.display = 'none';
    }

});




function ELEC_GREEN_ENERGY_REQUIREMENTChanged() {

    if ($('#DetailsView2_SelELEC_GREEN_ENERGY_REQUIREMENT').val() == '') {
        $('#DetailsView2_ELEC_GREEN_ENERGY_PERCENT').hide();
        $('#DetailsView2_lblELEC_GREEN_ENERGY_PERCENT').hide();
       
    }
    else if ($('#DetailsView2_SelELEC_GREEN_ENERGY_REQUIREMENT').val() == 'Yes') {
        $('#DetailsView2_ELEC_GREEN_ENERGY_PERCENT').show();
        $('#DetailsView2_lblELEC_GREEN_ENERGY_PERCENT').show();
    }



}

function GAS_CUSTOMER_AUTHChanged() {

    if ($('#DetailsView3_SelGAS_CUSTOMER_AUTH').val() == '') {
        $('#DetailsView3_btnRPF').hide();
        $('#DetailsView3_btnAddRPF').hide();
        $('#DetailsView3_GAS_CUSTOMER_AUTH_DATE').hide();
        $('#DetailsView3_lblGAS_CUSTOMER_AUTH_DATE').hide();
        
    }
    else if ($('#DetailsView3_SelGAS_CUSTOMER_AUTH').val() == 'Yes') {
        $('#DetailsView3_btnRPF').show();
        $('#DetailsView3_btnAddRPF').show();
        $('#DetailsView3_GAS_CUSTOMER_AUTH_DATE').show();
        $('#DetailsView3_lblGAS_CUSTOMER_AUTH_DATE').show();
    }



}



function ELEC_CUSTOMER_AUTHChanged() {

    if ($('#DetailsView2_SelELEC_CUSTOMER_AUTH').val() == '') {
        $('#DetailsView2_btnRPF').hide();
        $('#DetailsView2_btnAddRPF').hide();
        $('#DetailsView2_ELEC_CUSTOMER_AUTH_DATE').hide();
        $('#DetailsView2_lblELEC_CUSTOMER_AUTH_DATE').hide();
        
    }
    else if ($('#DetailsView2_SelELEC_CUSTOMER_AUTH').val() == 'Yes') {
        $('#DetailsView2_btnRPF').show();
        $('#DetailsView2_btnAddRPF').show();
        $('#DetailsView2_ELEC_CUSTOMER_AUTH_DATE').show();
        $('#DetailsView2_lblELEC_CUSTOMER_AUTH_DATE').show();
    }




}

function btnAuthChanged() {
    $("#DetailsView1_AUTH_TITLE").val($('#DetailsView1_TITLE').val());
    $("#DetailsView1_AUTH_FIRST_NAME").val($('#DetailsView1_FIRST_NAME').val());
    $("#DetailsView1_AUTH_LAST_NAME").val($('#DetailsView1_LAST_NAME').val());
    $("#DetailsView1_AUTH_POSITION").val($('#DetailsView1_POSITION').val());
}


function btnBillChanged() {
    $("#DetailsView1_BILL_ADDRESS").val($('#DetailsView1_NOTICE_ADDRESS').val());
    $("#DetailsView1_BILL_SUBURB").val($('#DetailsView1_NOTICE_SUBURB').val());
    $("#DetailsView1_SelBILL_STATE").val($('#DetailsView1_SelNOTICE_STATE').val());
    $("#DetailsView1_BILL_POSTCODE").val($('#DetailsView1_NOTICE_POSTCODE').val());

}

function selOPERATORChanged() {
    $("#DetailsView1_OPERATOR").val($('#DetailsView1_selOPERATOR').val());
}

function SelELEC_CURRENT_RETAILERChanged() {
    $("#DetailsView2_ELEC_CURRENT_RETAILER").val($('#DetailsView2_SelELEC_CURRENT_RETAILER').val());
}

function SelGAS_CURRENT_RETAILERChanged() {
    $("#DetailsView3_GAS_CURRENT_RETAILER").val($('#DetailsView3_SelGAS_CURRENT_RETAILER').val());
}

function SelSTTS_WINNER_ELECChanged() {
    $("#DetailsView1_STTS_WINNER_ELEC").val($('#DetailsView1_SelSTTS_WINNER_ELEC').val());
}

function SelSTTS_WINNER_GASChanged() {
    $("#DetailsView1_STTS_WINNER_GAS").val($('#DetailsView1_SelSTTS_WINNER_GAS').val());
}


function PromotionCodeChanged() {
    $("#DetailsView1_PromotionCode").val($('#DetailsView1_selPromotionCode').val());
}

function SelELEC_CONTRACT_TERMChanged() {

    var raw = $("#DetailsView2_ELEC_CONTRACT_START_DATE").val();
    
    var temp = new Array();
    temp = raw.split("-");
    var yrs = $("#DetailsView2_SelELEC_CONTRACT_TERM").val();
    temp[0] = parseInt(temp[0]);
    yrs = parseInt(yrs);

    if (!isNaN(yrs) && $('#DetailsView2_ELEC_ALTER_END_DATE').val()=="") {
        temp[0] = temp[0] + yrs;
        var cook = temp.join("-");
        $("#DetailsView2_ELEC_CONTRACT_END_DATE").val(cook);
    }
}


function ELEC_ALTER_END_DATEChanged() {
    $("#DetailsView2_ELEC_CONTRACT_END_DATE").val($('#DetailsView2_ELEC_ALTER_END_DATE').val());
}


function SelGAS_CONTRACT_TERMChanged() {

    var raw = $("#DetailsView3_GAS_CONTRACT_START_DATE").val();

    var temp = new Array();
    temp = raw.split("-");
    var yrs = $("#DetailsView3_SelGAS_CONTRACT_TERM").val();
    temp[0] = parseInt(temp[0]);
    yrs = parseInt(yrs);

    if (!isNaN(yrs) && $('#DetailsView3_GAS_ALTER_END_DATE').val() == "") {
        temp[0] = temp[0] + yrs;
        var cook = temp.join("-");
        $("#DetailsView3_GAS_CONTRACT_END_DATE").val(cook);
    }
}


function GAS_ALTER_END_DATEChanged() {
    $("#DetailsView3_GAS_CONTRACT_END_DATE").val($('#DetailsView3_GAS_ALTER_END_DATE').val());
}

function Copy_CNChanged(id) {
    var raw = id;
    var temp = new Array();
    temp = raw.split("_");
    temp[3] = "COMPANY";
    temp[4] = "NAME";
    var newid = temp.join("_");
    document.getElementById(newid).value = $("#DetailsView1_COMPANY_NAME").val();


}


function Copy_SDChanged(id) {
    var raw = id;
    var temp = new Array();
    temp = raw.split("_");
    temp[3] = "START";
    temp[4] = "DATE";
    var newid = temp.join("_");
    document.getElementById(newid).value = $("#DetailsView2_ELEC_CONTRACT_START_DATE").val();


}



function Copy_EDChanged(id) {
    var raw = id;
    var temp = new Array();
    temp = raw.split("_");
    temp[3] = "END";
    temp[4] = "DATE";
    var newid = temp.join("_");
    document.getElementById(newid).value = $("#DetailsView2_ELEC_CONTRACT_END_DATE").val();


}

function SelSITE_STATEChanged(id) {
    var raw = id;
    var temp = new Array();
    temp = raw.split("_");
    temp[3] = "PA";
    temp[4] = "SH";
    var shid = temp.join("_");

    if ($("#" + id).val() == 'NSW') {
        $("#" + shid).show();
    }
    else {
        $("#" + shid).val("");
        PA_TOTALChanged(shid);
        $("#" + shid).hide();
    }



}

function PA_TOTALChanged(id) {


    var raw = id;

    var temp = new Array();
    temp = raw.split("_");

    temp[4] = "TOTAL";
    var totalid = temp.join("_");
    temp[4] = "PK";
    var pkid = temp.join("_");
    temp[4] = "SH";
    var shid = temp.join("_");
    temp[4] = "OP";
    var opid = temp.join("_");


    var sum = parseInt("0");

    var pk = parseInt(document.getElementById(pkid).value);

    if (!isNaN(pk)) {
        sum = sum + pk;
    }

    var sh = parseInt(document.getElementById(shid).value);

    if (!isNaN(sh)) {
        sum = sum + sh;
    }

    var op = parseInt(document.getElementById(opid).value);

    if (!isNaN(op)) {
        sum = sum + op;
    }


    //document.getElementById(totalid).val(sum);
    //$("#DetailsView2_GridView2_ctl02_PA_TOTAL").val(sum);

    document.getElementById(totalid).value = sum;
    //document.getElementById(lblid).innerHTML = sum;

}


function ABNChanged(id) {


    var checkStr = document.getElementById(id).value;
    //var abn_fmt_str = (fmt_str) ? ‘\nThe ABN is a unique 11 digit number formed from a 9 digit unique identifier and two prefix check digits.\nThe Australian Business Register structure an ABN as XX XXX XXX XXX to assist readability.’ : ”;
    if (checkStr.length != 11 && checkStr.length != 14) {
        alert("ABN Must be 11 or 14 digits long");
    }
    else {
        var ch1 = parseInt(checkStr.charAt(0));
        var ch2 = parseInt(checkStr.charAt(1));
        var ch3 = parseInt(checkStr.charAt(2));
        var ch4 = parseInt(checkStr.charAt(3));
        var ch5 = parseInt(checkStr.charAt(4));
        var ch6 = parseInt(checkStr.charAt(5));
        var ch7 = parseInt(checkStr.charAt(6));
        var ch8 = parseInt(checkStr.charAt(7));
        var ch9 = parseInt(checkStr.charAt(8));
        var ch10 = parseInt(checkStr.charAt(9));
        var ch11 = parseInt(checkStr.charAt(10));

        var sum = 10 * (ch1 - 1) + ch2 + 3 * ch3 + 5 * ch4 + 7 * ch5 + 9 * ch6 + 11 * ch7 + 13 * ch8 + 15 * ch9 + 17 * ch10 + 19 * ch11;

        var chk = sum % 89;

        if (chk != 0) {
            alert("ABN is NOT valid!");
            document.getElementById(id).focus();
        }

    }

}


function NMIChanged(id) {
    var checkStr = document.getElementById(id).val();
    if (checkStr.length != 11) {
        alert("NMI Must be 11 digits long");
    }
    else {
        var hash1 = { 0: "12", 1: "13", 2: "5", 3: "6", 4: "7", 5: "8", 6: "9", 7: "10", 8: "11", 9: "12" };
        var hash2 = { 0: "15", 1: "17", 2: "1", 3: "3", 4: "5", 5: "7", 6: "9", 7: "2", 8: "4", 9: "6" };

        var ch1 = parseInt(hash1[checkStr.charAt(0)]);
        var ch2 = parseInt(hash2[checkStr.charAt(1)]);
        var ch3 = parseInt(hash1[checkStr.charAt(2)]);
        var ch4 = parseInt(hash2[checkStr.charAt(3)]);
        var ch5 = parseInt(hash1[checkStr.charAt(4)]);
        var ch6 = parseInt(hash2[checkStr.charAt(5)]);
        var ch7 = parseInt(hash1[checkStr.charAt(6)]);
        var ch8 = parseInt(hash2[checkStr.charAt(7)]);
        var ch9 = parseInt(hash1[checkStr.charAt(8)]);
        var ch10 = parseInt(hash2[checkStr.charAt(9)]);
        var ch11 = parseInt(checkStr.charAt(10));

        var sum = ch1 + ch2 + ch3 + ch4 + ch5 + ch6 + ch7 + ch8 + ch9 + ch10;
        var chk = sum % 10 + ch11;

        if (chk != 10) {
            alert("NMI is NOT valid!" + chk);
            document.getElementById(id).focus();
        }
    }
}


function CheckText(e, text) {
    if (e.value == text) {
        e.value = '';
    }
}


function selSTATChanged(id,tag) {
    //if ($('#DetailsView1_selELEC_STAT').val() == 'Customer lost  have accepted offer with another retailer') {
    if (document.getElementById(tag).value == 'Customer lost  have accepted offer with another retailer' || document.getElementById(tag).value == 'Customer converted / accepted offer' || document.getElementById(tag).value == 'Customer currently with another retailer') {
        document.getElementById(id).style.display = 'inline';
    }
    else {
        document.getElementById(id).style.display = 'none';
    }

}



